package cn.gnp.framework.eeamis.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import cn.gnp.framework.persist.Constants;
import cn.gnp.framework.persist.dao.hibernate.BaseHibernateDao;
import cn.gnp.framework.eeamis.dao.goods.IGoodsCodeDao;
import cn.gnp.framework.eeamis.model.goods.GoodsCodeModel;

@Repository
public class GoodsCodeHibernateDao extends BaseHibernateDao<GoodsCodeModel, Integer> implements IGoodsCodeDao {

    public List<GoodsCodeModel> listAllByGoods(int pn, int goodsId) {
        final String hql = getListAllHql() + " where goods.id = ?";
        return list(hql, pn, Constants.DEFAULT_PAGE_SIZE , goodsId);
    }

    public int countAllByGoods(int goodsId) {
        final String hql = getCountAllHql() + " where goods.id = ?";
        Number result = unique(hql, goodsId);
        return result.intValue();
    }

    public GoodsCodeModel getOneNotExchanged(int goodsId) {
        String hql = getListAllHql() + " where goods.id = ? and exchanged=false";
        List<GoodsCodeModel> result = list(hql, goodsId);
        if(CollectionUtils.isEmpty(result)) {
            return null;
        }
        return result.get(0);
    }



}
